package com.cool.dto.response;

import java.io.Serializable;

import com.power.common.constants.BaseErrorCode;
import com.power.common.interfaces.IMessage;
import com.power.common.model.BaseResult;

/**
 * Common Result
 *
 * @author b1an
 */
public class CommonResult<T> extends BaseResult<T> implements Serializable {

    /**
     * Default constructor
     */
    public CommonResult() {

    }

    /**
     * @param success the success
     * @param message the message
     */
    public CommonResult(boolean success, String message) {
        setSuccess(success);
        setMessage(message);
    }

    /**
     * @param success the success
     */
    public CommonResult(boolean success) {
        setSuccess(success);
    }

    /**
     * @param code error code
     * @param message success or error messages
     */
    public CommonResult(String code, String message) {
        setCode(code);
        setMessage(message);
    }

    /**
     * @param success the success
     * @param message success or error messages
     * @param data response data while true
     */
    public CommonResult(boolean success, String message, T data) {
        setSuccess(success);
        setMessage(message);
        setData(data);
    }

    /**
     * Successful response (default response code is 0000)
     *
     * @return CommonResult
     */
    public static CommonResult ok() {
        return ok(BaseErrorCode.Common.SUCCESS);
    }

    /**
     * Customize success response message, generally define enumeration to implement IMessage
     *
     * @param message IMessage interface
     * @param <T> Object
     * @return CommonResult
     */
    public static <T> CommonResult<T> ok(IMessage message) {
        return baseCreate(message.getCode(), message.getMessage(), true);
    }

    /**
     * Server unknown exception (default response code 9999)
     *
     * @return CommonResult
     */
    public static CommonResult fail() {
        return fail(BaseErrorCode.Common.UNKNOWN_ERROR);
    }

    /**
     * Failed response
     *
     * @param message IMessage
     * @return CommonResult
     */
    public static CommonResult fail(IMessage message) {
        return fail(message.getCode(), message.getMessage());
    }

    /**
     * 失败或失败响应
     *
     * @param code the error code
     * @param message error message
     * @return CommonResult
     */
    public static CommonResult fail(String code, String message) {
        return baseCreate(code, message, false);
    }

    private static <T> CommonResult<T> baseCreate(String code, String msg, boolean success) {
        CommonResult result = new CommonResult();
        result.setCode(code);
        result.setSuccess(success);
        result.setMessage(msg);
        return result;
    }

    /**
     * Usage: Result.ok().setResult("hello") Business data returned when processing is successful
     *
     * @param data you return data
     * @return CommonResult
     */
    public CommonResult<T> setResult(T data) {
        setData(data);
        return this;
    }

    /**
     * Usage: Result.ok().addData("hello") Business data returned when processing is successful
     *
     * @param data you return data
     * @return CommonResult
     */
    public CommonResult<T> addData(T data) {
        setData(data);
        return this;
    }

    /**
     * Override the supper method
     *
     * @return T
     */
    @Override
    public T getData() {
        return (T)super.getData();
    }

}
